package com.zhengqing.system.util;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.zhengqing.common.core.util.ApplicationContextHelper;
import com.zhengqing.system.config.EmailProperties;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @ClassName EmailUtil
 * @Description TODO
 * @Author Des1gn
 * @Date 2023/4/11 9:18
 * @Version 1.0
 **/
@Slf4j
public class EmailUtil {

    private final static MailAccount mailAccount = new MailAccount();

    static {
        EmailProperties emailProperties = ApplicationContextHelper.popBean(EmailProperties.class);
        mailAccount.setHost(emailProperties.getHost());
        mailAccount.setPass(emailProperties.getPassword());
        mailAccount.setFrom(emailProperties.getAddress());
        mailAccount.setUser(emailProperties.getAddress());
        mailAccount.setStarttlsEnable(emailProperties.isStarttlsEnable());
        mailAccount.setSslEnable(emailProperties.isSslEnable());
        mailAccount.setPort(emailProperties.getPort());
        mailAccount.setAuth(emailProperties.isAuth());
    }

    public static void sendFileEmail(String toUser, String title, String content, File file){
        String messageId = MailUtil.send(mailAccount, toUser, title, content, false, file);
        log.info("发送邮件回调messageId-> {}",messageId);
    }

    public static void sendHtmlEmail(String toUser, String title, String content){
        String messageId = MailUtil.send(mailAccount, toUser, title, content, true);
        log.info("发送邮件回调messageId-> {}",messageId);
    }

}
